<template>
	<view>
		<!-- #ifdef MP-WEIXIN -->
		<zero-privacy :onNeed='false'></zero-privacy>
		<!-- #endif -->
		<view class="content">
			<view class="" style="margin-bottom: 50rpx;">
				<view :style="{top: statusBarHeight+10 +'px'}">
					<u-icon name="close" size="30" @click="navTo"></u-icon>
				</view>
			</view>
			<view class="logo flex justify-center">
				<u-image width="300rpx" :show-menu-by-longpress="false" src="https://ssjh.jifenma.com/assets/img/logo_gxxq.png"
					mode="widthFix"></u-image>
			</view>
			<!-- style="padding-top: 100rpx;" -->
			<view class="flex flex-column align-center" style="padding-top: 0;">
				<!-- <dy-button width="576rpx" size="default" color="#fff" :backgroundColor="'var(--mainColor)'"
					@click="login">微信授权登录</dy-button> -->
				<button v-if="showPhone" open-type="getPhoneNumber" style="border-radius: 40rpx; font-size: 30;"
					:style="{width: '576rpx',color: '#fff',backgroundColor:'var(--mainColor)'}"
					@getphonenumber="bindPhone">手机号绑定</button>
				<button v-if="!showPhone" style="border-radius: 40rpx; font-size: 30;"
					:style="{width: '576rpx',color: '#fff',backgroundColor:'var(--mainColor)'}" @click.stop="login"
					open-type="agree">手机号一键登录</button>
				<dy-button v-if="phoneLogin" class="u-m-t-20" width="576rpx" size="default" color="#333333"
					backgroundColor="#f8f8f8" borderColor="#dedede" @click="tologin">手机号登录/注册</dy-button>
				<input type="text" placeholder="请输入推荐人号码" v-model="tjid"
					style="border: 1px solid #fff; margin-top: 30rpx;border-radius: 15rpx;" class="u-p-l-15">
				<view class="u-m-t-30">
					<u-checkbox shape="circle" v-model="checked">
						<text>请阅读以下条款</text>
						<text class="text-primary" @click.stop="toArgu('register_credit')">《用户服务协议》</text>
					</u-checkbox>
					<view class="text-center">
						<text class="text-primary" @click.stop="toArgu('privacy_agreement')">《隐私政策》</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		BaseCenterApi
	} from "@/api/serves.js"
	import {
		mapMutations,
		mapActions
	} from 'vuex'
	import {
		baseUrl
	} from "@/api/app.js"
	export default {
		data() {
			return {
				checked: false,
				loginCode: "",
				phoneNum: '',
				showPhone: true,
				tjid: '',
				phoneLogin: false,
				isPay: 0 , //是否是从扫码支付页过来的  0 不是   1：是
				tjuid: 0,
				type: ''
			}
		},
		onLoad(option) {
			this.phoneLogin = 'http://fly.a6shop.net' == baseUrl
			console.log(baseUrl, '请求路径', 'http://fly.a6shop.net' == baseUrl);
			this.tjid = option.id;
			let tjid = uni.getStorageSync("tjid")
			if (tjid) {
				this.tjid = tjid
				uni.removeStorageSync("tjid")
			}
			if (option.tjuid) {
				this.tjuid = option.tjuid
				this.type = option.type
			}
			const { q ,isPay } = option
			console.log('option', option);
			if (isPay) this.isPay = isPay
			if (q) {
				let urlStr = decodeURIComponent(q)
				let urlParams = this.getUrlParams(urlStr)
				let code = urlParams.id
				this.tjid = code
			}
		},
		onReady() {
			this.getCode()
		},
		methods: { //参数解析
			getUrlParams(url) {
				let o = {};
				if (url.indexOf("?") != -1) {
					let str = url.substr(url.indexOf("?") + 1).replace(/[#/|/#/]/g, "");
					// console.log(str);       
					let strs = str.split("&");
					// console.log(strs);      
					for (let i = 0; i < strs.length; i++) {
						o[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
					}
				}
				return o;
			},
			...mapMutations({
				SET_USER_INFO: 'user/SET_USER_INFO',
				SET_USER_TOKEN: 'user/SET_USER_TOKEN',
			}),
			...mapActions({
				getUserInfo: 'user/getUserInfo'
			}),
			navTo() {
				uni.switchTab({
					url: "/pages/tabbar/index/index"
				})
			},
			tologin() {
				uni.navigateTo({
					url: "/pages/login/login/login",
				});
			},
			getCode() {
				uni.login({
					provider: 'weixin',
					success: res => {
						console.log("进来先拿到code", res);
						this.loginCode = res.code
					},
					fail: err => {
						console.log(err);
					}
				});
			},
			async login() {
				// 用于微信登录   判断是否点击
				if (!this.checked) return this.$Toast('是否查看并同意隐私条款', 1500)
				console.log("有没有进来");

				if (this.loginCode) {
					uni.getUserProfile({
						desc: '用于完善个人资料',
						lang: "zh_CN",
						success: async (res) => {
							let code = this.loginCode
							// const pary ={
							// 	code: this.loginCode, // 是	string	小程序登录时获取的 code
							// 	rawData: JSON.parse(res.rawData), // 是	string	小程序用户信息的rawData
							// 	tjid: this.tjid, // 否	string	推荐人用户名
							// 	mobile:this.phoneNum,
							// }
							// console.log("发送的数据",pary);
							console.log("user profile", res.userInfo)
							// if(ares)
							// uni.login({
							// 	provider: 'weixin',
							// 	success: async lres => { 
							// 		this.loginCode = lres.code
							// 		let code = this.loginCode
							// 		const mres = await BaseCenterApi.appWechatLogin({ rawData: res.rawData, code })
							// 		console.log("mres>>>", mres);
							// 		if (mres.code == 1) {
							// 			uni.removeStorageSync("tjid")
							// 			this.SET_USER_TOKEN(mres.data.token)
							// 			this.getUserInfo()
							// 			uni.switchTab({
							// 				url: '/pages/tabbar/index/index'
							// 			})
							// 		}
							// 	} 
							// }); 
							;
							console.log('请求参数', {
								code: this.loginCode, // 是	string	小程序登录时获取的 code
								rawData: res.userInfo, // 是	string	小程序用户信息的
								tjid: this.tjid, // 否	string	推荐人用户名
								mobile: this.phoneNum,
							});
							const ares = await BaseCenterApi.authLogin({
								code: this.loginCode, // 是	string	小程序登录时获取的 code
								rawData: res.userInfo, // 是	string	小程序用户信息的
								tjid: this.tjid, // 否	string	推荐人用户名
								mobile: this.phoneNum,
								tjuid: this.tjuid	//推荐人id
							})
							console.log('登录信息',ares);
							if (ares.code == 1) {
								uni.removeStorageSync("tjid")
								this.SET_USER_TOKEN(ares.data.token)
								this.getUserInfo()
								if(this.isPay == '1'){
									this.isPay = 0
									uni.navigateBack()
								}else if (this.tjid) {
									uni.switchTab({
										url: '/pages/tabbar/mine/mine'
									})
								} else {
									uni.navigateBack()
								}
								return
							}

							console.log("ares>>>", ares);
						},
						fail: err => {
							console.log(err);
						}
					});

				} else {
					this.getCode()
				}
			},
			toArgu(type) {
				uni.navigateTo({
					url: '/pages/login/registerArgu/registerArgu?type=' + type
				})
			},
			bindPhone(e) {
				if (!this.checked) return this.$Toast('请同意隐私协议', 1500)
				console.log(e, '获取的手机号');
				if (e.detail.errMsg === "getPhoneNumber:ok") {
					// return
					BaseCenterApi.newGetMobil({
						code: e.detail.code
					}).then((res) => {
						if (!res.code == 1) this.$Toast('请重新绑定手机号', 1500)
						this.showPhone = false;
						this.$Toast('绑定成功', 800);
						this.phoneNum = res.data;
						// if(baseUrl === 'http://fly.a6shop.net'){
						// 	this.phoneNum = '18574718289'
						// }
						// console.log('phone',this.phoneNum);
						console.log("绑定返回的手机号", this.phoneNum, res.data);
					}).catch((err) => {
						this.$Toast('请重新绑定手机号', 1500)
					})
				}

			}
		},
	}
</script>

<style lang="scss" scoped>
	.logo {
		margin: 200rpx auto;
		width: 530rpx;
	}

	.content {
		padding: 120rpx 74rpx 0;
	}
</style>